package com.yang;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Objects;


public class B {
    private FileWriter fileWriter = null;
    private BufferedWriter bufferedWriter = null;
    protected void write(String writeText){
        File file = new File(A.TMP); //获取文件

        //文件不存在创建文件..
        if (!file.exists()){
            System.out.println("文件不存在开始创建文件~~~~~~~~~~~");
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
            System.out.println("文件创建成功~~~~~~~~~~");
        }
        System.out.println("开始写入数据~~~~~~~~~~");
        //写入数据
        try {
            fileWriter = new FileWriter(file);
            bufferedWriter = new BufferedWriter(fileWriter);
            bufferedWriter.write(writeText);
            bufferedWriter.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                if (Objects.nonNull(bufferedWriter)){
                    bufferedWriter.close();
                }
                if (Objects.nonNull(fileWriter)){
                    fileWriter.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        System.out.printf("写入数据完成~~~~~~~~~~");
    }
}
